<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style>
        .top {
            float: right;
            position: relative;
            top: -25px;
            background-color: rgb(39, 39, 112);
        }

        .btn-primary {
            margin-left: 10px;
        }

        .price {
            width: 100px;
            height: 34px;
            outline: none;
            border: 1px solid rgb(173, 171, 171);
            border-radius: 4px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">后台管理</h3>
                <a href="sel.html"><button class="btn btn-primary top">查看租借情况</button></a>
                <a href="sell.html"><button class="btn btn-primary top">查看出售情况</button></a>
            </div>
            <div class="panel-body form-inline">
                <label for="">
                    类名:
                    <input type="text" v-model="object" class="form-control">
                </label>
                <button @click="add" class="btn btn-primary">添加</button>
            </div>
        </div>

        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>类别</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td> {{item.id}} </td>
                    <td> {{item.object}} </td>
                </tr>
            </tbody>
            <span> 共 {{list.length}} 类 </span>
        </table>
    </div>
</body>
<script src="js/vue.js"></script>
<script src="js/vue-resource.min.js"></script>
<script>
    Vue.http.options.emulateJSON = true;

    var vm = new Vue({
        el: "#app",
        data: {
            object: '',
            list: []
        },
        created() {
            this.getAllList();
        },
        methods: {
            getAllList: function () {
                this.$http.get('php/object.php').then(function (result) {
                    this.list = result.body;
                })
            },
            add: function () {
                this.$http.post('php/creatObject.php', {
                    object: this.object,
                }, {
                    emulateJSON: true
                }).then(function (result) {
                    if (result.code == 0) {
                        //成功了,添加完成后再调用一下getAllList()
                        alert(result.body.message);
                        // this.getAllList();
                    } else {
                        alert(result.body.message)
                    }
                })
                this.getAllList();
            }
        }
    })
</script>

</html>